package cn.ming.wfchat.app.me.activity;

import android.annotation.SuppressLint;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;
import cn.ming.wfchat.R;
import cn.ming.wfchat.app.AppService;
import cn.ming.wfchat.app.dialog.ExchangeFjListDialog;
import cn.ming.wfchat.app.dialog.PaymentDialog;
import cn.ming.wfchat.app.me.activity.model.TransferAccoountBean;
import cn.ming.wfchat.kit.WfcBaseActivity;
import cn.ming.wfchat.kit.utils.Tools;
import cn.wildfirechat.remote.ChatManager;

/**
 * 兑换
 */
public class ExchangeActivity extends WfcBaseActivity {


    @BindView(R.id.tv_keyong)
    TextView tvKeyong;
    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.ll_usdt)
    LinearLayout llUsdt;
    @BindView(R.id.edit_query)
    EditText editQuery;
    @BindView(R.id.tv_fj_amount)
    TextView tvFjAmount;
    @BindView(R.id.tv_dsc)
    TextView tvDsc;
    @BindView(R.id.tv_fj)
    TextView tvFj;
    @BindView(R.id.cb_check)
    CheckBox cbCheck;
    @BindView(R.id.tv_shouxufei_text)
    TextView tvShouxufeiText;
    @BindView(R.id.tv_shouxufei)
    TextView tvShouxufei;
    @BindView(R.id.tv_shiji_text)
    TextView tvShijiText;
    @BindView(R.id.tv_shiji)
    TextView tvShiji;
    @BindView(R.id.tv_next)
    TextView tvNext;
    private String coinType;
    private BigDecimal rete;

    @Override
    protected void afterViews() {
        super.afterViews();
        initDate();
        setTitle("兑换");

    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_exchange;
    }

    void initDate() {
        coinType = getIntent().getStringExtra("coinType");
        toTransferAccount(coinType);
        editQuery.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String editable = editQuery.getText().toString();
                String regEx = "\\.\\d{6}";  //只能输入字母或数字
                Pattern p = Pattern.compile(regEx);
                Matcher m = p.matcher(editable);
                String str = m.replaceFirst("").trim();    //删掉不是字母或数字的字符
                if (!editable.equals(str)) {
                    editQuery.setText(str);  //设置EditText的字符
                    editQuery.setSelection(str.length()); //因为删除了字符，要重写设置新的光标所在位
                    Tools.showToast(ExchangeActivity.this, "小数点后输入不能超过5位");
                }
                try {
                    BigDecimal amoutf = new BigDecimal(str);
                    tvShiji.setText(amoutf.multiply(rete) + "FJ");
                    tvFjAmount.setText(amoutf.multiply(rete).toPlainString());
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        llUsdt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ExchangeFjListDialog.newInstance().setOnClickListener(new ExchangeFjListDialog.OnClickListener() {
                    @Override
                    public void erc() {
                        coinType = "ETH_USDT";
                        toTransferAccount(coinType);
                    }

                    @Override
                    public void trc() {
                        coinType = "TRX_USDT";
                        toTransferAccount(coinType);
                    }
                }).show(ExchangeActivity.this);
            }
        });


    }

    @OnClick(R.id.tv_next)
    void toTransfer() {
        if (TextUtils.isEmpty(editQuery.getText().toString().trim())) {
            Tools.showToast(ExchangeActivity.this, "请输入金额");
            return;
        }
        PaymentDialog.newInstance("兑换", editQuery.getText().toString().trim() + coinType).setOnClickListener(new PaymentDialog.OnClickListener() {
            @Override
            public void getPwd(String password) {
                AppService.Instance().exchangeFjSave(editQuery.getText().toString().trim(), password, coinType, cbCheck.isChecked(),
                        new AppService.SendCodeCallback() {
                            @Override
                            public void onUiSuccess() {
                                finish();
                                ChatManager.showToast("兑换成功");
                            }

                            @Override
                            public void onUiFailure(String msg) {
                                ChatManager.showToast(msg);
                            }
                        });
            }
        }).show(ExchangeActivity.this);

    }

    void toTransferAccount(String type) {
        AppService.Instance().exchangeFj(type, new AppService.TransferAccoountCallback() {
            @Override
            public void onUiSuccess(TransferAccoountBean rechargeBean) {
                if (type.contains("ETH")) {
                    tvKeyong.setText("可用资产（ USDT-ERC20）");
                    tvType.setText("USDT-ERC20");
                    tvDsc.setText(" (FJ:USDT-ERC20) 1:" + rechargeBean.getUsdtFjNum().toPlainString());
                    tvShouxufei.setText(rechargeBean.getFee().toPlainString() + "ETH");
                } else {
                    tvKeyong.setText("可用资产（ USDT-TRC20）");
                    tvType.setText("USDT-TRC20");
                    tvDsc.setText(" (USDT-TRC20:FJ) 1:" + rechargeBean.getUsdtFjNum().toPlainString());
                    tvShouxufei.setText(rechargeBean.getFee().toPlainString() + "TRX");
                }
                tvAmount.setText(rechargeBean.getAmount().toPlainString());
                tvFj.setText("可用" + rechargeBean.getPledgeAmount() + "USDT");
                rete = rechargeBean.getUsdtFjNum();
                cbCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        if (b) {
                            tvShouxufei.setText(rechargeBean.getPledgeAmount() + "USDT");
                        } else {
                            if (coinType.contains("ETH")) {
                                tvShouxufei.setText(rechargeBean.getFee().toPlainString() + "ETH");
                            } else {
                                tvShouxufei.setText(rechargeBean.getFee().toPlainString() + "TRX");
                            }
                        }
                    }
                });
            }

            @Override
            public void onUiFailure(int code, String msg) {
                ChatManager.showToast(msg);
            }
        });
    }

}
